<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 收藏
 *
 */
class Collection extends CI_Controller {
	function __construct()
	{
		parent::__construct();
		//加载usermodel
		$this->load->model('usermodel');
		$this->uid = $this->usermodel->get_id_by_token($this->input->post('token'));
		$this->load->model('collectionmodel');
	}
	
	function index()
	{
		$this->mycollections();
	}
	
	/**
	 * 我的收藏列表
	 *
	 */
	function mycollections()
	{
		header('Content-type:text/xml');// 向浏览器输出 xml
		$collections = $this->_get_collections($this->uid);
		if(count($collections) > 0){
			//输出最终的xml
			echo $this->xmllib->get_xml($collections, array('collection'), 'collections');
			exit;
		}else{
			//结果集为空
			echo $this->xmllib->empty_xml();
			exit;
		}
	}
	
	/**
	 * 查看别人的收藏列表
	 *
	 */
	function other_mycollections()
	{
		header('Content-type:text/xml');// 向浏览器输出 xml
		$uid = $this->input->post('uid');
		$collections = $this->_get_collections($uid);
		if(count($collections) > 0){
			//输出最终的xml
			echo $this->xmllib->get_xml($collections, array('collection'), 'collections');
			exit;
		}else{
			//结果集为空
			echo $this->xmllib->empty_xml();
			exit;
		}
	}
	
	/**
	 * 添加收藏
	 *
	 * @param unknown_type $id
	 */
	function addcollection()
	{
		header('Content-type:text/xml');// 向浏览器输出 xml
		
		$collection_id = $this->input->post('collectionid');//要收藏的应用或专辑id
		$collection_type = $this->input->post('collectiontype');//要收藏的应用或专辑 1 应用 2 专辑
		if(empty($collection_id) || !in_array($collection_type, array(ALBUM_COLLECTION, APP_COLLECTION))){
			echo $this->xmllib->error_xml('非法操作，收藏失败');
			exit;
		}
		
		if($this->collectionmodel->check_collection($collection_id, $this->uid, $collection_type)){
			//要收藏的应用已经收藏过了
			echo $this->xmllib->error_xml('此应用已经收藏过，请不要重复收藏');
			exit;
		}
		
		if($collection_type == APP_COLLECTION){
			//如果是收藏应用
			/*$this->load->model('appmodel');
			if(!$this->appmodel->check_app($collection_id, $this->uid, 0)){
				//要收藏的应用不存在或者是用户自己的
				echo $this->xmllib->error_xml('此应用无效，无法收藏');
				exit;
			}*/
			//通过检测，执行收藏操作
			if($this->collectionmodel->insert_collection($this->uid, $collection_id, $collection_type)){
				//添加动态
				$this->load->model('activemodel');
				$this->activemodel->insert_active(array('uid' 			=> $this->uid,
														'active_id'		=> $collection_id,
														'active_type'	=> APP_ACTIVE,
														'memo'			=> '收藏了'));
				echo $this->xmllib->success_xml('收藏成功');
				exit;
			}else{
				//添加收藏失败
				echo $this->xmllib->error_xml('收藏失败，请稍候再试');
				exit;
			}
		}
		elseif ($collection_type == ALBUM_COLLECTION){
			//如果是收藏专辑
			/*$this->load->model('albummodel');
			if(!$this->albummodel->check_album($collection_id, $this->uid, 0)){
				//要收藏的应用不存在或者是用户自己的
				echo $this->xmllib->error_xml('此专辑无效，无法收藏');
				exit;
			}*/
			//通过检测，执行收藏操作
			if($this->collectionmodel->insert_collection($this->uid, $collection_id, $collection_type)){
				//添加动态
				$this->load->model('activemodel');
				$this->activemodel->insert_active(array('uid' 			=> $this->uid,
														'active_id'		=> $collection_id,
														'active_type'	=> ALBUM_ACTIVE,
														'memo'			=> '收藏了'));
				echo $this->xmllib->success_xml('收藏成功');
				exit;
			}else{
				//添加收藏失败
				echo $this->xmllib->error_xml('收藏失败，请稍候再试');
				exit;
			}
		}
		else{
			//添加收藏失败
			echo $this->xmllib->error_xml('收藏失败，请稍候再试');
			exit;
		}
	}
	
	/**
	 * 删除收藏
	 *
	 * @param unknown_type $id
	 */
	function delcollection()
	{
		header('Content-type:text/xml');// 向浏览器输出 xml
		$id = $this->input->post('id');
		if (empty($id)) {
			//非法操作
			echo $this->xmllib->error_xml('不允许的操作');
			exit;
		}
		//检测传递的应用id是否有效
		//加载app model
		$this->load->model('appmodel');
		if(!$this->collectionmodel->check_collection_2($id, $this->uid)){
			//会员没有收藏此用户的话，不能删除
			echo $this->xmllib->error_xml('不允许的操作');
			exit;
		}
		
		//执行删除操作
		if ($this->collectionmodel->delete_collection($id)) {
			echo $this->xmllib->success_xml('删除成功');
			exit;
		}else{
			//删除失败
			echo $this->xmllib->success_xml('删除失败，请稍候再试');
			exit;
		}
	}
	
	//获取收藏信息，包括应用和专辑
	function _get_collections($uid='')
	{
		$this->dblib->set_query_limit();
		$this->db->select('c.collection_type, c.collection_id, c.createtime')
				 ->from('collection as c')
				 ->where('c.uid', $uid)
				 ->order_by('c.createtime', 'desc');
		$query = $this->db->get();
		$collections = $query->result_array();
		//加载图片路径处理类库
		$this->load->library('uploadimglib');
		foreach ($collections as $k=>$v){
			//查找专辑中应用和专辑
			if($v['collection_type'] == ALBUM_COLLECTION){
				//是专辑
				$this->db->select('a.id as album_id, a.name, a.createtime as album_createtime, a.lastupdate as album_lastupdate, u.name as user_name, u.id as user_id')
						 ->from('album as a')
						 ->join('user as u', 'u.id=a.uid')
						 ->where('a.id', $v['collection_id'])
						 ->limit(1);
				$query = $this->db->get();
				$albuminfo = $query->row_array();
				$collections[$k] = array_merge($collections[$k], $albuminfo);
				//评论
	    		$this->db->select('count(*) as commentscount')
	    				 ->from('comments')
	    				 ->where(array('uid' => $uid, 'comment_type' => ALBUM_COMMENT));
	    		$query = $this->db->get();
	    		$res = $query->row_array();
	    		$collections[$k]['commentscount'] = $res['commentscount'];
	    		unset($res);
	    		
	    		//收藏
	    		$this->db->select('count(*) as collectioncount')
	    				 ->from('collection')
	    				 ->where(array('uid' => $uid, 'collection_type' => ALBUM_COLLECTION));
	    		$query = $this->db->get();
	    		$res = $query->row_array();
	    		$collections[$k]['collectioncount'] = $res['collectioncount'];
	    		unset($res);
			}
			elseif ($v['collection_type'] == APP_COLLECTION){
				//是应用
				$this->db->select('a.id as app_id, a.name, a.photo, a.sharedtime, a.lastupdate as app_lastupdate, u.name as user_name, u.id as user_id')
						 ->from('app as a')
						 ->join('user as u', 'u.id=a.uid')
						 ->where('a.id', $v['collection_id'])
						 ->limit(1);
				$query = $this->db->get();
				$appinfo = $query->row_array();
				isset($appinfo['photo']) && $appinfo['photo'] = $this->uploadimglib->re_image_path($appinfo['photo']);//处理图片路径
				$collections[$k] = array_merge($collections[$k], $appinfo);
			}
		}
		
		return $collections;
	}
}


/* End of file collection.php */
/* Location: ./application/controllers/collection.php */